home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pccts / dlgautoh.fix < prev    next >
Text File  |  1993-07-19  |  9KB  |  398 lines

  1. /* dlgauto.h automaton
  2.  *
  3.  * SOFTWARE RIGHTS
  4.  *
  5.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  6.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  7.  * company may do whatever they wish with source code distributed with
  8.  * PCCTS or the code generated by PCCTS, including the incorporation of
  9.  * PCCTS, or its output, into commerical software.
  10.  * 
  11.  * We encourage users to develop software with PCCTS.  However, we do ask
  12.  * that credit is given to us for developing PCCTS.  By "credit",
  13.  * we mean that if you incorporate our source code into one of your
  14.  * programs (commercial product, research project, or otherwise) that you
  15.  * acknowledge this fact somewhere in the documentation, research report,
  16.  * etc...  If you like PCCTS and have developed a nice tool with the
  17.  * output, please mention that you developed it using PCCTS.  In
  18.  * addition, we ask that this header remain intact in our source code.
  19.  * As long as these guidelines are kept, we expect to continue enhancing
  20.  * this system and expect to make other tools available as they are
  21.  * completed.
  22.  *
  23.  * ANTLR 1.06
  24.  * Will Cohen
  25.  * Purdue University
  26.  * 1989-1992
  27.  */
  28.  
  29. char    *zzlextext;    /* text of most recently matched token */
  30. char    *zzbegexpr;    /* beginning of last reg expr recogn. */
  31. char    *zzendexpr;    /* beginning of last reg expr recogn. */
  32. int    zzbufsize;    /* number of characters in zzlextext */
  33. int    zzbegcol = 0;    /* column that first character of token is in*/
  34. int    zzendcol = 0;    /* column that last character of token is in */
  35. int    zzline = 1;    /* line current token is on */
  36. int    zzchar;        /* character to determine next state */
  37. int    zzbufovf;    /* indicates that buffer too small for text */
  38. int    zzcharfull = 0;
  39. static char    *zznextpos;/* points to next available position in zzlextext*/
  40. static int     zzclass;
  41.  
  42. #ifdef __STDC__
  43. void    zzerrstd(char *);
  44. void    (*zzerr)(char *)=zzerrstd;/* pointer to error reporting function */
  45. #else
  46. void    zzerrstd();
  47. void    (*zzerr)()=zzerrstd;    /* pointer to error reporting function */
  48. #endif
  49.  
  50. static FILE    *zzstream_in;
  51. extern int    zzerr_in();
  52. static int    (*zzfunc_in)() = zzerr_in;
  53.  
  54. static int     zzauto = 0;
  55. static int    zzadd_erase;
  56. static char     zzebuf[70];
  57.  
  58. #ifdef ZZCOL
  59. #define ZZINC (++zzendcol)
  60. #else
  61. #define ZZINC
  62. #endif
  63.  
  64.  
  65. #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
  66.  
  67. #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
  68.  
  69. void
  70. #ifdef __STDC__
  71. zzrdstream( FILE *f )
  72. #else
  73. zzrdstream( f )
  74. FILE *f;
  75. #endif
  76. {
  77.     /* make sure that it is really set to something, otherwise just 
  78.        leave it be.
  79.     */
  80.     if (f){
  81.         /* make sure that there is always someplace to get input
  82.            before closing zzstream_in
  83.         */
  84.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  85.         zzline = 1;
  86.         zzstream_in = f;
  87.         zzfunc_in = NULL;
  88.         zzcharfull = 0;
  89.     }
  90. }
  91.  
  92. void
  93. #ifdef __STDC__
  94. zzrdfunc( int (*f)() )
  95. #else
  96. zzrdfunc( f )
  97. int (*f)();
  98. #endif
  99. {
  100.     /* make sure that it is really set to something, otherwise just 
  101.        leave it be.
  102.     */
  103.     if (f){
  104.         /* make sure that there is always someplace to get input
  105.            before closing zzstream_in
  106.         */
  107.         if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
  108.         zzline = 1;
  109.         zzstream_in = NULL;
  110.         zzfunc_in = f;
  111.         zzcharfull = 0;
  112.     }
  113. }
  114.  
  115.  
  116. void
  117. zzclose_stream()
  118. {
  119. #if 0
  120.     fclose( zzstream_in );
  121.     zzstream_in = NULL;
  122.     zzfunc_in = NULL;
  123. #endif
  124. }
  125.  
  126. /* saves dlg state, but not what feeds dlg (such as file position) */
  127. void
  128. #ifdef __STDC__
  129. zzsave_dlg_state(struct zzdlg_state *state)
  130. #else
  131. zzsave_dlg_state(state)
  132. struct zzdlg_state *state;
  133. #endif
  134. {
  135.     state->func_ptr = zzfunc_in;
  136.     state->stream = zzstream_in;
  137.     state->lookc = zzchar;
  138.     state->char_full = zzcharfull;
  139.     state->begcol = zzbegcol;
  140.     state->endcol = zzendcol;
  141.     state->line = zzline;
  142. }
  143.  
  144. void
  145. #ifdef __STDC__
  146. zzrestore_dlg_state(struct zzdlg_state *state)
  147. #else
  148. zzrestore_dlg_state(state)
  149. struct zzdlg_state *state;
  150. #endif
  151. {
  152.     zzfunc_in = state->func_ptr;
  153.     zzstream_in = state->stream;
  154.     zzchar = state->lookc;
  155.     zzclass = ZZSHIFT(zzchar);
  156.     zzcharfull = state->char_full;
  157.     zzbegcol = state->begcol;
  158.     zzendcol = state->endcol;
  159.     zzline = state->line;
  160. }
  161.  
  162. void
  163. #ifdef __STDC__
  164. zzmode( int m )
  165. #else
  166. zzmode( m )
  167. int m;
  168. #endif
  169. {
  170.     /* points to base of dfa table */
  171.     if (m<MAX_MODE){
  172.         zzauto = m;
  173.         /* have to redo class since using different compression */
  174.         zzclass = ZZSHIFT(zzchar);
  175.     }else{
  176.         sprintf(zzebuf,"Invalid automaton mode = %d ",m);
  177.         zzerr(zzebuf);
  178.     }
  179. }
  180.  
  181. /* erase what is currently in the buffer, and get a new reg. expr */
  182. void
  183. zzskip()
  184. {
  185.     zzadd_erase = 1;
  186. }
  187.  
  188. /* don't erase what is in the zzlextext buffer, add on to it */
  189. void
  190. zzmore()
  191. {
  192.     zzadd_erase = 2;
  193. }
  194.  
  195. /* substitute c for the reg. expr last matched and is in the buffer */
  196. #ifdef __STDC__
  197. void
  198. zzreplchar(char c)
  199. #else
  200. void
  201. zzreplchar(c)
  202. char c;
  203. #endif
  204. {
  205.     /* can't allow overwriting null at end of string */
  206.     if (zzbegexpr < &zzlextext[zzbufsize-1]){
  207.         *zzbegexpr = c;
  208.         *(zzbegexpr+1) = '\0';
  209.     }
  210.     zzendexpr = zzbegexpr;
  211.     zznextpos = zzbegexpr + 1;
  212. }
  213.  
  214. /* replace the string s for the reg. expr last matched and in the buffer */
  215. void
  216. #ifdef __STDC__
  217. zzreplstr(register char *s)
  218. #else
  219. zzreplstr(s)
  220. register char *s;
  221. #endif
  222. {
  223.     register char *l= &zzlextext[zzbufsize -1];
  224.  
  225.     zznextpos = zzbegexpr;
  226.     if (s){
  227.          while ((zznextpos <= l) && (*(zznextpos++) = *(s++))){
  228.             /* empty */
  229.         }
  230.         /* correct for NULL at end of string */
  231.         zznextpos--;
  232.     }
  233.     if ((zznextpos <= l) && (*(--s) == 0)){
  234.         zzbufovf = 0;
  235.     }else{
  236.         zzbufovf = 1;
  237.     }
  238.     *(zznextpos) = '\0';
  239.     zzendexpr = zznextpos - 1;
  240. }
  241.  
  242. void
  243. zzgettok()
  244. {
  245.     register int state, newstate;
  246.     /* last space reserved for the null char */
  247.     register char *lastpos;
  248.  
  249. skip:
  250.     zzbufovf = 0;
  251.     lastpos = &zzlextext[zzbufsize-1];
  252.     zznextpos = zzlextext;
  253.     zzbegcol = zzendcol+1;
  254. more:
  255.     zzbegexpr = zznextpos;
  256. #ifdef ZZINTERACTIVE
  257.     /* interactive version of automaton */
  258.     /* if there is something in zzchar, process it */
  259.     state = newstate = dfa_base[zzauto];
  260.     if (zzcharfull){
  261.         ZZINC;
  262.         /* Truncate matching buffer to size (not an error) */
  263.         if (zznextpos < lastpos){
  264.             *(zznextpos++) = zzchar;
  265.         }else{
  266.             zzbufovf = 1;
  267.         }
  268.         newstate = dfa[state][zzclass];
  269.     }
  270.     if (zzstream_in)
  271.         while (zzalternatives[newstate]){
  272.             state = newstate;
  273.             ZZGETC_STREAM;
  274.             ZZINC;
  275.             /* Truncate matching buffer to size (not an error) */
  276.             if (zznextpos < lastpos){
  277.                 *(zznextpos++) = zzchar;
  278.             }else{
  279.                 zzbufovf = 1;
  280.             }
  281.             newstate = dfa[state][zzclass];
  282.         }
  283.     else if (zzfunc_in)
  284.         while (zzalternatives[newstate]){
  285.             state = newstate;
  286.             ZZGETC_STREAM;
  287.             ZZINC;
  288.             /* Truncate matching buffer to size (not an error) */
  289.             if (zznextpos < lastpos){
  290.                 *(zznextpos++) = zzchar;
  291.             }else{
  292.                 zzbufovf = 1;
  293.             }
  294.             newstate = dfa[state][zzclass];
  295.         }
  296.     /* figure out if last character really part of token */
  297.     if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
  298.         zzcharfull = 1;
  299.         --zznextpos;
  300.     }else{
  301.         zzcharfull = 0;
  302.         state = newstate;
  303.     }
  304.     *(zznextpos) = '\0';
  305.     /* Able to transition out of start state to some non err state?*/
  306.     if ( state == dfa_base[zzauto] ){
  307.         /* make sure doesn't get stuck */
  308.         zzadvance();
  309.     }
  310. #else
  311.     /* non-interactive version of automaton */
  312.     if (!zzcharfull)
  313.         zzadvance();
  314.     else
  315.         ZZINC;
  316.     state = dfa_base[zzauto];
  317.     if (zzstream_in)
  318.         while ((newstate = dfa[state][zzclass]) != DfaStates){
  319.             state = newstate;
  320.             /* Truncate matching buffer to size (not an error) */
  321.             if (zznextpos < lastpos){
  322.                 *(zznextpos++) = zzchar;
  323.             }else{
  324.                 zzbufovf = 1;
  325.             }
  326.             ZZGETC_STREAM;
  327.             ZZINC;
  328.         }
  329.     else if (zzfunc_in)
  330.         while ((newstate = dfa[state][zzclass]) != DfaStates){
  331.             state = newstate;
  332.             /* Truncate matching buffer to size (not an error) */
  333.             if (zznextpos < lastpos){
  334.                 *(zznextpos++) = zzchar;
  335.             }else{
  336.                 zzbufovf = 1;
  337.             }
  338.             ZZGETC_FUNC;
  339.             ZZINC;
  340.         }
  341.     zzcharfull = 1;
  342.     if ( state == dfa_base[zzauto] ){
  343.         if (zznextpos < lastpos){
  344.             *(zznextpos++) = zzchar;
  345.         }else{
  346.             zzbufovf = 1;
  347.         }
  348.         *zznextpos = '\0';
  349.         /* make sure doesn't get stuck */
  350.         zzadvance();
  351.     }else{
  352.         *zznextpos = '\0';
  353.     }
  354. #endif
  355. #ifdef ZZCOL
  356.     zzendcol -= zzcharfull;
  357. #endif
  358.     zzendexpr = zznextpos -1;
  359.     zzadd_erase = 0;
  360.     (*actions[accepts[state]])();
  361.     switch (zzadd_erase) {
  362.         case 1: goto skip;
  363.         case 2: goto more;
  364.     }
  365. }
  366.  
  367. void
  368. zzadvance()
  369. {
  370.     if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
  371.     if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
  372.     if (!(zzstream_in || zzfunc_in)){
  373.         zzerr_in();
  374.     }
  375. }
  376.  
  377. void
  378. #ifdef __STDC__
  379. zzerrstd(char *s)
  380. #else
  381. zzerrstd(s)
  382. char *s;
  383. #endif
  384. {
  385.         fprintf(stderr,
  386.                 "%s near line %d (text was '%s')\n",
  387.                 ((s == NULL) ? "Lexical error" : s),
  388.                 zzline,zzlextext);
  389. }
  390.  
  391. int
  392. zzerr_in()
  393. {
  394.     fprintf(stderr,"No input stream or function\n");
  395.     /* return eof to get out gracefully */
  396.     return -1;
  397. }
  398.